package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.*;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lhh
 * @ClassName HouseController
 * @date 2022/9/23 18:07
 */
@RestController
@RequestMapping("house")
public class HouseController {

    @Reference
    private HouseService houseService;
    
    @Reference
    private CommunityService communityService;
    
    @Reference
    private HouseBrokerService houseBrokerService;

    @Reference
    private HouseImageService houseImageService;

    @Reference
    private UserFollowService userFollowService;

    @PostMapping("list/{pageNum}/{pageSize}")
    public Result<PageInfo<HouseVo>> list(@PathVariable Integer pageNum,
                                          @PathVariable Integer pageSize,
                                          @RequestBody HouseQueryVo houseQueryVo){
        PageInfo<HouseVo> page= houseService.findListPage(pageNum,pageSize,houseQueryVo);
        return Result.ok(page);
    }

    @GetMapping("info/{id}")
    public Result getById(@PathVariable Long id, HttpServletRequest request){

        Map map = new HashMap();
        //房源信息
        House house = houseService.getById(id);
        map.put("house",house);
        //小区信息
        Community community = communityService.getById(house.getCommunityId());
        map.put("community",community);
        //经纪人信息 前端显示第一个经纪人信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        map.put("houseBrokerList",houseBrokerList);
        //房源图片信息
        List<HouseImage> houseImage1List = houseImageService.findList(id, 1);
        map.put("houseImage1List",houseImage1List);
        //是否关注
        boolean isFollow=false;
        UserInfo userInfo= (UserInfo) request.getSession().getAttribute("USER");
        if(null!=userInfo){
            Long userId = userInfo.getId();
            isFollow=userFollowService.isFollow(userId,id);
        }
        map.put("isFollow",isFollow);
        return Result.ok(map);
    }
}
